# -*- coding: utf-8 -*-

from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import GdkPixbuf

import model
import os

from model.adapters.toquinmodelgtk import ToquinModelGtk

class VtnBase(Gtk.Builder):
    parent_window = None
    main_widget_name = None
    
    def __init__(self, main_widget_name, parent_window):
        Gtk.Builder.__init__(self)
        
        self.main_widget_name = main_widget_name
        self.parent_window = parent_window
        
        self.add_from_file("ui/%s.ui" % main_widget_name)
        
        if self.get_object("btn_volver"):
            self.get_object("btn_volver").connect('clicked', self.switch_to_parent)

        if self.get_object("btn_casa"):
            self.get_object("btn_casa").connect('clicked', self.switch_to_main)
            
        self.__dict__[main_widget_name] = self.get_object(main_widget_name)
        self.connect_signals(self)
        
    def hide(self):
        self.get_object(self.main_widget_name).hide()
        
    def show(self):
        self.get_object(self.main_widget_name).show()
        
    def switch_to_parent(self, sender):
        if self.parent_window:
            self.parent_window.show()
            self.hide()
            
    def switch_to_main(self, sender):
        if self.parent_window:
            self.parent_window.switch_to_main(sender)
            self.hide()
        else:
            self.show()
            
    def show_background_image(self, background_name):
        # From http://www.gtkforums.com/viewtopic.php?p=195278#p195278
        provider = Gtk.CssProvider()
        display = Gdk.Display.get_default()
        screen = display.get_default_screen()
        
        Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
        
        provider.load_from_data("GtkWindow { background: url('./media/images/backgrounds/%s.png'); }" % background_name)
        
    def fill_treeview_with_model(self, treeview, filterkeyword = None):
        gtk_toquin_model = ToquinModelGtk(model.current_model, filterkeyword)
        
        treeview.set_model(gtk_toquin_model)   
 
        rendererToggle = Gtk.CellRendererToggle()
        rendererToggle.connect('toggled', gtk_toquin_model.markForInstallation)
        columna = Gtk.TreeViewColumn("Marcar", rendererToggle, active = 0)
        treeview.append_column(columna)
        
        rendererText = Gtk.CellRendererText()
        columna = Gtk.TreeViewColumn("Programa", rendererText, text = 1)
        treeview.append_column(columna)
        
        columna = Gtk.TreeViewColumn("Descripción", rendererText, text = 2)
        treeview.append_column(columna)

    def selected_package_changed(self, sender):
        #Config
        images_location = "/home/david/Projects/toquin-files/images"
        
        (instalable_model, instalable_iter_path) = self.treeview_instalables.get_selection().get_selected()
        
        if not instalable_iter_path:
            return        

        selected_item = instalable_model[instalable_iter_path]
        image_path = os.path.join(images_location, "%s.png" % selected_item.id)
        description_text = ""

        self.img_icon.clear()

        if os.path.exists(image_path):
            img_pixbuf = GdkPixbuf.new_from_file(image_path)
            img_pixbuf_width = float(img_pixbuf.get_width())
            img_pixbuf_height = float(img_pixbuf.get_height())
            img_icon_rect = self.img_icon.get_allocation()
            scaled_image = None

            if (float(img_pixbuf_width) / float(img_pixbuf_height)) > (img_icon_rect.width / img_icon_rect.height):
                scaled_image = img_pixbuf.scale_simple(int(img_icon_rect.width), int(img_pixbuf_height * img_icon_rect.width / img_pixbuf_width), Gdk.INTERP_BILINEAR)
            else:
                scaled_image = img_pixbuf.scale_simple(int(img_pixbuf_width * img_icon_rect.height / img_pixbuf_height), int(img_icon_rect.height), Gdk.INTERP_BILINEAR)

            self.img_icon.set_from_pixbuf(scaled_image)
            
        if selected_item.description:
            description_text = selected_item.description

        self.textview_descripcion.get_buffer().set_text(description_text)
        
    def __getattr__(self, name):
        if name in self.__dict__:
            return self.__dict__[name]
        else:
            widget = self.get_object(name)
            
            if widget:
                return widget
            
            raise AttributeError()
